﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Data;

/*
Developed By: Bhupender Bindra
Developed On: 23th Nov 2010
Description: The code provides the user to download the files in zip folder.   
*/

namespace DownloadContentKit.Classes
{
    public class AdminDownloadLibrary
    {
        /// <summary>
        /// get the list name
        /// </summary>
        public string ListName
        {
            get
            {
                return "Download Library";
            }
        }

        /// <summary>
        /// fetch all the record in the list and fill in data set
        /// </summary>
        /// <returns></returns>
        public DataSet GetListData()
        {
            DataSet ds = new DataSet();
            try
            {
                DataTable dt;
                dt = new DataTable();
                DataColumn dc = new DataColumn("ID");
                dt.Columns.Add(dc);
                dc = new DataColumn("Title");
                dt.Columns.Add(dc);
                dc = new DataColumn("Status");
                dt.Columns.Add(dc);
                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        SPList myList = web.Lists[ListName.ToString()];
                        SPQuery query = new SPQuery();
                        query.Query = "<OrderBy><FieldRef Name='ID' Ascending='True'/></OrderBy>";
                        SPListItemCollection items = myList.GetItems(query);
                        foreach (SPListItem item in items)
                        {
                            DataRow dr = dt.NewRow();
                            dr["ID"] = item["ID"].ToString();
                            dr["Title"] = item["Title"].ToString();
                            dr["Status"] = item["Status"].ToString();
                            dt.Rows.Add(dr);
                        }
                    }
                }
                ds.Tables.Add(dt);
            }
            catch { }
            return ds;
        }

        /// <summary>
        /// Create Download library list
        /// </summary>
        public bool AddDownloadLibraryList()
        {
            bool _isListStatus = true;
            try
            {
                Boolean _isListExists;
                //SPWeb myWeb = SharepointManager.GetCurrentWeb();
                AdminDownloadLibrary objList = new AdminDownloadLibrary();
                _isListExists = SharepointManager.ListExists(SPContext.Current.Web, objList.ListName.ToString());
                //check list is avaliable or not
                if (!_isListExists)
                {
                    //create a list in share point
                    SharepointManager.CreateNewList(objList.ListName.ToString(), objList.ListName.ToString(), SPListTemplateType.GenericList);
                }
                using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb myDifferentWeb = spSite.OpenWeb())
                    {
                        myDifferentWeb.AllowUnsafeUpdates = true;
                        SPList list = myDifferentWeb.Lists[objList.ListName.ToString()];
                        //Quick launch true means list name display in left menu in share point
                        list.OnQuickLaunch = false;

                        Boolean _isFieldExists = list.Fields.ContainsField("Status");

                        if (list != null && !_isFieldExists)
                        {
                            list.Fields.Add("Status", SPFieldType.Text, true);
                            list.Update();

                            SPView view = list.DefaultView;
                            view.ViewFields.Add("Status");
                            view.Update();

                        }
                    }
                }
                _isListStatus = true;
            }
            catch
            {
                _isListStatus = false;
            }
            return _isListStatus;
        }
    }
}
